package com.bitdf.txing.search.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.valid.UpdateGroup;
import com.bitdf.txing.search.service.GroupUpdateService;
import com.bitdf.txing.search.to.GroupInfoTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Lizhiwei
 * @date 2023/1/26 20:53:15
 * 注释：
 */
@RequestMapping("${txing.request.path.search}/group")
@RestController
@Slf4j
public class GroupUpdateController {

    @Autowired
    GroupUpdateService groupUpdateService;

    /**
     * 组团点赞
     * @param groupId
     * @param count
     * @return
     * @throws IOException
     */
//    @GetMapping("/modify/likecount")
//    public R modifyLikeCount(@RequestParam("groupId") Long groupId,@RequestParam("count") Long count) throws IOException {
//        Boolean aBoolean=groupUpdateService.modifyLikeCount(groupId,count);
//        return aBoolean?R.ok():R.error(BizCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getCode(), BizCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getMsg());
//    }

//    @GetMapping("/modify/viewcount")
//    public R modifyViewCount(@RequestParam("groupId") Long groupId) throws IOException {
//        Boolean b =groupUpdateService.modifyViewCount(groupId);
//        return b?R.ok():R.error(BizCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getCode(), BizCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getMsg());
//    }

    /**
     * 关闭组团
     *
     * @param groupId
     * @return
     * @throws IOException
     */
    @SaCheckLogin
    @GetMapping("/close")
    public R closeGroup(@RequestParam("groupId") Long groupId) throws IOException {
        Boolean b = groupUpdateService.modifyStatus(groupId);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION);
    }

    /**
     * 更新组团信息中具有数量属性的一些字段
     * 例如：
     *
     * @param groupId
     * @return
     * @throws IOException
     */
    @SaCheckLogin
    @GetMapping("/count/modify/increOrDcre")
    public R groupInfoIncreOrDcre(@RequestParam("groupId") Long groupId, @RequestParam("opsValue") Integer opsValue, @RequestParam("target") String target) throws IOException {
        Boolean b = groupUpdateService.increOrDcre(groupId, opsValue, target);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getCode(), TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getMsg());
    }

    /**
     * 更新组团信息
     *
     * @param groupInfoTo
     * @return
     */
    @SaCheckLogin
    @PostMapping("/update")
    public R updateGroup(@Validated(UpdateGroup.class) @RequestBody GroupInfoTo groupInfoTo, BindingResult result) throws IOException {
        // 判断校验是否成功
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
        // 发生了修改 修改标志位设置为 1
        groupInfoTo.setModifyFlag(1);
        boolean b = groupUpdateService.updateGroup(groupInfoTo);
        log.info("组团id： {} 修改成功： {}", groupInfoTo.getId(), b);
        return b ? R.ok() : R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getCode(), TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION.getMsg());
    }
}
